<?php

	class Devmod_method_castField extends Devmod_methods_abstraction {
		
		protected $after_operation = array(array(
										'for_method' => 'export',
										'for_method_type' => 'edit',
										'use' => 'eval_edit'
									), array(
										'for_method' => 'export',
										'for_method_type' => 'list',
										'use' => 'eval_list'
									), array(
										'for_method' => 'export',
										'for_method_type' => 'frontend_list',
										'use' => 'eval_frontend_list'
									),array(
										'for_method' => 'export',
										'for_method_type' => 'frontend_add',
										'use' => 'eval_add'
									),array(
										'for_method' => 'export',
										'for_method_type' => 'add',
										'use' => 'eval_add'
									));
		
		/**
		 * @param $args [array]
		 * 0 = $field_name
		 * 1 = $display_type
		 * 2 [optional] = $args
		 */
		public function set_config($args) 
		{ 
			
			if(is_array($args[0]))
			{
				$field_name = implode(";", $args[0]);
			} else {
				$field_name = $args[0];
			}
			
			$this->update_config('cast', array($field_name => array(
				'display_type' => $args[1],
				'field_name' => $field_name,
				'args' => (isset($args[2])) ? $args[2] : null
			)));
			
			if($args[1] == "date")
			{ 
				$this->cross_call('callFunction', array(
					$args[0],
					array(
						'function' => 'cast_date_convert',
						'args' => array('%%value%%') // %%value%% is the current value for the field while saving
					)
				));
			}
		}
		
		// ADD
		public function eval_add($args)
		{
			if(!isset($this->config[$this->type]['cast'])) return $args;

			foreach($args as $field => $value)
			{
				if(array_key_exists($field, $this->config[$this->type]['cast']))
				{
					$args[$field] = $this->call_class($field, $value, $args, $field);
				}
			}
			
			return $args;
		}
		
		// EDIT
		public function eval_edit($args)
		{
			if(!isset($this->config['edit']['cast'])) return $args;
			
			$return = array();

			foreach($args as $field => $value)
			{
				if(array_key_exists($field, $this->config['edit']['cast']))
				{
					$return[$field] = $this->call_class($field, $value, $args, $field);
				} else {
					$return[$field] = $value;
				}
			}
			return $this->helper->array_to_object($return);
		}
		
		// LIST
		public function eval_list($args)
		{
			if(!isset($this->config['list']['cast'])) return $args;
			
			$return = array();
			
			foreach($args as $row_key => $row_value)
			{
				foreach($row_value as $field => $value)
				{
					if(array_key_exists($field, $this->config['list']['cast']))
					{
						$return[$row_key][$field] = $this->call_class($field, $value, $row_value, $field);
					} else {
						$return[$row_key][$field] = $value;
					}
				}
			}
			return $this->helper->array_to_object($return);
		}
		
		// FRONTEND_LIST
		public function eval_frontend_list($args)
		{
			if(!isset($this->config['frontend_list']['cast'])) return $args;
			
			$done_calls = array();
			
			$return = array();
			
			foreach($args as $row_key => $row_value)
			{
				foreach($row_value as $field => $value)
				{
					if(array_key_exists($field, $this->config['frontend_list']['cast']))
					{
						$return[$row_key][$field] = $this->call_class($field, $value, $row_value, $field);
						
						$done_calls[] = $field; // new for the multi row castFields
					} else {
						$return[$row_key][$field] = $value;
					}
					
				}
			}
			
			// new multi row castFields for frontend_lists
			foreach($this->config['frontend_list']['cast'] as $k => $v)
			{
				if(!in_array($k, $done_calls))
				{
					$childs = explode(";", $k);
					foreach($return as $rk => $rv)
					{
						// vars decleration
						$value = $return[$rk][$childs[0]][$childs[1]];
						$row_value = $return[$rk][$childs[0]];
						$field = $childs[0];
						
						// write back informations
						$return[$rk][$childs[0]][$childs[1]] = $this->call_class($field, $value, $row_value, $k);
					}	
				}
			}
			
			return $this->helper->object_to_array($return);
		}
		
		// CALL THE TYPE CLASS
		private function call_class($field, $value, $row_value, $config_field)
		{
			$_cfg = $this->config[$this->type]['cast'][$config_field];
			
			$_call_class = "Devmod_method_castField_{$_cfg['display_type']}";
			
			$_obj = new $_call_class($value, $row_value, $_cfg, $this->config, $this->view);
			
			if(method_exists($_obj, "type_{$this->type}"))
			{
				return call_user_func_array(array($_obj, "type_{$this->type}"), array());
			} else {
				$err = new Yomm_Error("[devmod error: castField class type '{$_call_class}' does not have 'type_{$this->type}' method]");
				echo $err->getMessage();
			}
		}
	}